package com.kongwg.exer1;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author kongwg
 * @create 2020-12-15 10:36
 */
public class EmployeeTest {
    @Test
    public void test1(){
        TreeSet tree = new TreeSet();
        tree.add(new Employee("小孔",18,new MyDate(1997,9,8)));
        tree.add(new Employee("大王",14,new MyDate(1993,9,8)));
        tree.add(new Employee("懒猪",16,new MyDate(1992,9,8)));
        tree.add(new Employee("肥卓",12,new MyDate(1999,9,8)));
        tree.add(new Employee("孬李",20,new MyDate(1990,9,8)));

        Iterator iterator = tree.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    @Test
    public void test2(){
        Comparator comparator = new Comparator() {

            //按生日先后排序
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee && o2 instanceof Employee){
                    Employee e1 = (Employee) o1;
                    Employee e2 = (Employee) o2;

                    MyDate b1 = e1.getBirthday();
                    MyDate b2 = e2.getBirthday();

//                    //比较年
//                    int minusYear = b1.getYear() - b2.getYear();
//                    if (minusYear != 0){
//                        return minusYear;
//                    }
//                    //比较月
//                    int minusMonth = b1.getMonth() - b2.getMonth();
//                    if (minusMonth != 0){
//                        return minusMonth;
//                    }
//                    //比较日
//                    return b1.getDay() - b2.getDay();

                    return b1.compareTo(b2);

                }

            throw new RuntimeException("传入的数据类型不一致！");
            }

        };
        TreeSet tree = new TreeSet(comparator);
        tree.add(new Employee("小孔",18,new MyDate(1997,9,8)));
        tree.add(new Employee("大王",14,new MyDate(1993,10,2)));
        tree.add(new Employee("懒猪",16,new MyDate(1992,2,9)));
        tree.add(new Employee("肥卓",12,new MyDate(1999,5,8)));
        tree.add(new Employee("孬李",20,new MyDate(1990,2,1)));

        Iterator iterator = tree.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

}
